package com.xb.cloud.shopping.client;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.shopping.client.vo.ShoppingCartQueryVo;
import com.xb.cloud.shopping.service.ShoppingCartApiService;

@Component
public class ShoppingCartAdapter extends Adapter {

	private final Logger logger = LoggerFactory.getLogger(ShoppingCartAdapter.class);

	@Resource
	ShoppingCartApiService shoppingCartApiService;

	/**
	 * 
	 * <p>
	 * Title: execute
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param urlPath
	 * @param isLogined
	 * @param bizInfo
	 * @param context
	 * @return
	 * @see com.xb.cloud.gateway.client.Adapter#execute(java.lang.String,
	 *      java.lang.Boolean, java.util.Map,
	 *      com.xb.cloud.gateway.client.AdapterContext)
	 */
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		long time = System.currentTimeMillis();

		RtnInfo rtnInfo = null;
		long companyId = context.getCompanyId();
		long currentUserId = context.getCurrentUserId();
		
		if ("/h5/shopping/cart/getUserCartInfo".equalsIgnoreCase(urlPath)) {
			ShoppingCartQueryVo userShoppingCartVo = new ShoppingCartQueryVo();
			userShoppingCartVo.setCompanyId(companyId);
			userShoppingCartVo.setCurrentUserId(currentUserId);
			return shoppingCartApiService.getUserCartInfo(userShoppingCartVo);
		}

		
		if ("/h5/shopping/cart/addSku".equalsIgnoreCase(urlPath)) {
			Integer num = Integer.valueOf(bizInfo.get("num"));
			Long skuId = Long.valueOf(bizInfo.get("skuId"));
			ShoppingCartQueryVo userShoppingCartVo = new ShoppingCartQueryVo();
			userShoppingCartVo.setCompanyId(companyId);
			userShoppingCartVo.setCurrentUserId(currentUserId);
			userShoppingCartVo.setSkuId(skuId);
			userShoppingCartVo.setNum(num);
			return shoppingCartApiService.addSkuToCart(userShoppingCartVo);
		}

		if ("/h5/shopping/cart/updateNum".equalsIgnoreCase(urlPath)) {
			Integer num = Integer.valueOf(bizInfo.get("num"));
			Long skuId = Long.valueOf(bizInfo.get("skuId"));
			ShoppingCartQueryVo userShoppingCartVo = new ShoppingCartQueryVo();
			userShoppingCartVo.setCompanyId(companyId);
			userShoppingCartVo.setCurrentUserId(currentUserId);
			userShoppingCartVo.setSkuId(skuId);
			userShoppingCartVo.setNum(num);
			return shoppingCartApiService.updateItemNum(userShoppingCartVo);
		}

		if ("/h5/shopping/cart/getCartNum".equalsIgnoreCase(urlPath)) {
			ShoppingCartQueryVo userShoppingCartVo = new ShoppingCartQueryVo();
			userShoppingCartVo.setCompanyId(companyId);
			userShoppingCartVo.setCurrentUserId(currentUserId);
			return shoppingCartApiService.getCartNum(userShoppingCartVo);
		}

		if ("/h5/shopping/cart/deleteSkus".equalsIgnoreCase(urlPath)) {
			ShoppingCartQueryVo userShoppingCartVo = new ShoppingCartQueryVo();
			userShoppingCartVo.setCompanyId(companyId);
			userShoppingCartVo.setCurrentUserId(currentUserId);
			String skuIds = bizInfo.get("skuIds");
			
			return shoppingCartApiService.deleteSkus(userShoppingCartVo, skuIds);
		}

		logger.debug("执行时间：{}", System.currentTimeMillis() - time);
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/shopping/cart/**";
	}

	@Override
	public boolean isCheckUserToken() {
		return true;
	}
}